﻿var equipmentChangeFormVue = new Vue({
    el: "#equipmentChangeForm",
    data: {
        currentAssignerID: 0,
        currentManagerID: 0,
        equipmentList: [],
        userInfo: {
            ID: 0,
            Name: "",
            NameChs: "",
            Email: "",
            Mobile: ""
        },
        checkList: [],
        pageModel: {
            pageIdx: 1,
            pageSize: 100,
            pageTotal: 0,
            pageLength: 0,
            reults: []
        },
        searchModel: {
            queryAssignerID: 0,
            queryManagerID: 0,

            isAsc: true,
            isAll: false
        },
    },
    methods: {
        GetUserInfo: function () {
            GetSysUserByName(this, this.userInfo.Name);
        },
        SelectEquipment: function (event) {
            $(event.target).parent().find("input[type=checkbox]").click();
        },
        SelectAllEquipment: function (event) {
            var isCheckAll = $(event.target).prop("checked");
            $("input[type=checkbox]").each(function () {
                if ($(this).prop("checked") != isCheckAll) {
                    $(this).click();
                }
            })
        },
        ChangeEquipment: function (event) {
            event.preventDefault();
            if (this.currentAssignerID > 0) {
                ChangeEquipmentAssiger(this);
            } else if (this.currentManagerID > 0) {
                ChangeEquipmentManager(this);
            }
        }

    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.currentAssignerID = GetQueryString("AssignerID");
        this.currentManagerID = GetQueryString("ManagerID");

        if (this.currentAssignerID > 0) {
            ListEquipmentsByAssignerID(this, this.currentAssignerID);
        } else if (this.currentManagerID > 0) {
            ListEquipmentsByManagerID(this, this.currentManagerID);
        }
    }
})

/**
 * 列出负责人所有设备
 */
function ListEquipmentsByAssignerID(vue, assignerID) {

    vue.searchModel.queryAssignerID = assignerID;
    vue.searchModel.queryManagerID = 0;

    ResetForm(vue);
    parent.ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Equipment/ListEquipmentByManagerIDForPage?v=" + Math.random(),
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                vue.equipmentList = res.data;
                vue.$nextTick(function () {
                    AddSelectEquipmentEvent();
                    parent.ResizeAzModal();
                })
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            parent.HideAzLoading();
            parent.toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 列出管理人所有设备
 */
function ListEquipmentsByManagerID(vue, managerID) {

    vue.searchModel.queryAssignerID = 0;
    vue.searchModel.queryManagerID = managerID;

    ResetForm(vue);
    parent.ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Equipment/ListEquipmentByManagerIDForPage?v=" + Math.random(),
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                vue.pageModel = res.data;
                vue.equipmentList = vue.pageModel.results;
                console.log(res.equipmentList);
                vue.$nextTick(function () {
                    AddSelectEquipmentEvent();
                    parent.ResizeAzModal();
                })
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            parent.HideAzLoading();
            parent.toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 设置监听设备点击事件
 */
function AddSelectEquipmentEvent(vue) {
    $("input[type=checkbox]").click(function () {
        $(this).parent().parent().toggleClass("success");
    });
}

/**
 * 更新设备的负责人
 */
function ChangeEquipmentAssiger(vue) {
    ResetCheckList(vue);
    if (CheckForm(vue)) {
        var message = "确认将所选设备转移到负责人Confirm that the selected equipment is transferred to the person in charge【" + vue.userInfo.NameChs + "】?";
        if (confirm(message)) {
            parent.ShowAzLoading();
            $("button").attr("disabled", true);
            $.ajax({
                type: "POST",
                url: "/Equipment/UpdateEquipmentAssigner",
                data: {
                    AssignerID: vue.userInfo.ID,
                    EquipmentIDs: vue.checkList
                },
                success: function (res) {
                    if (res.status == "Success") {
                        toastr.success("设备转移成功Device transfer succeeded。");
                        parent.ResizeAzModal();
                        ListEquipmentsByAssignerID(vue, vue.currentAssignerID);
                    } else {
                        toastr.error(res.message);
                    }
                },
                error: function (err) {
                    console.log(err);
                    parent.toastr.error("服务器发生错误，请稍后再试或联系管理员An error occurred on the server. Please try again later or contact your administrator。");
                },
                complete: function () {
                    parent.HideAzLoading();
                    $("button").attr("disabled", true);
                }
            });
        }
    }
}

/**
 * 更新设备的管理人
 */
function ChangeEquipmentManager(vue) {
    ResetCheckList(vue);
    if (CheckForm(vue)) {
        var message = "确认将所选设备转移到负责人Confirm that the selected equipment is transferred to the person in charge【" + vue.userInfo.NameChs + "】?";
        if (confirm(message)) {
            parent.ShowAzLoading();
            $("button").attr("disabled", true);
            $.ajax({
                type: "POST",
                url: "/Equipment/UpdateEquipmentManager",
                data: {
                    ManagerID: vue.userInfo.ID,
                    EquipmentIDs: vue.checkList
                },
                success: function (res) {
                    if (res.status == "Success") {
                        toastr.success("设备转移成功Device transfer succeeded。");
                        parent.ResizeAzModal();
                        ListEquipmentsByManagerID(vue, vue.currentManagerID);
                    } else {
                        toastr.error(res.message);
                    }
                },
                error: function (err) {
                    console.log(err);
                    parent.toastr.error("服务器发生错误，请稍后再试或联系管理员An error occurred on the server. Please try again later or contact your administrator。");
                },
                complete: function () {
                    parent.HideAzLoading();
                    $("button").attr("disabled", true);
                }
            });
        }
    }
}

/**
 * 检查用户所填写内容
 */
function CheckForm(vue) {
    if (!vue.userInfo || !vue.userInfo.ID || vue.userInfo.ID == 0) {
        toastr.error("请填写新的负责人或管理人信息Please fill in the new person in charge or manager information。");
        return false;
    } else if (vue.checkList.length == 0) {
        toastr.error("请至少选择一个设备Please select at least one device。");
        return false;
    } else {
        return true;
    }
}

/**
 * 重置用户所选的设备列表
 */
function ResetCheckList(vue) {
    vue.checkList = [];
    $("input[type=checkbox]").each(function () {
        if ($(this).prop("checked")) {
            if (isNumber($(this).val())) {
                vue.checkList.push($(this).val());
            }
        }
    });
}

/**
 * 重置表单信息
 */
function ResetForm(vue) {
    vue.equipmentList = [];
    vue.userInfo = {
        ID: 0,
        Name: "",
        NameChs: "",
        Email: "",
        Mobile: ""
    }
    vue.checkList = [];
}

/**
 * 根据用户名查询用户信息
 */
function GetSysUserByName(vue, userName) {
    if (userName != null && userName.length > 0) {
        parent.ShowAzLoading();
        $.ajax({
            type: "GET",
            url: "/Account/GetUserByName?userName=" + userName + "&v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    //设置用户信息
                    vue.userInfo = res.data;
                } else {
                    toastr.error(res.message);
                }
                parent.HideAzLoading();
            },
            error: function (err) {
                parent.HideAzLoading();
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            }
        });
    } else {
        vue.userInfo = {};
    }
}

/**
 * 判断是否数字
 */
function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}